package org.codeart.visitor.example01;

import java.text.NumberFormat;
import java.time.LocalDate;

/**
 * 折扣计价访问者类
 */
public class DiscountVisitor implements Visitor {
    
    private final LocalDate billDate;

    public DiscountVisitor(LocalDate billDate) {
        this.billDate = billDate;
        System.out.println("结算日期: " + billDate);
    }

    @Override
    public void visit(Candy candy) {
        System.out.println("糖果: " + candy.getName());
        // 糖果生产超过 180 天禁止售卖, 否则糖果一律 9 折
        long gap = billDate.toEpochDay() - candy.getProduceDate().toEpochDay();
        if (gap > 180) {
            System.out.println("糖果超过保质期, 请勿食用!");
        } else {
            double finalPrice = candy.getPrice() * 0.9;
            System.out.printf("糖果打折后价格为: %s\n", NumberFormat.getCurrencyInstance().format(finalPrice));
        }
    }

    @Override
    public void visit(Fruit fruit) {
        System.out.printf("水果: %s\n", fruit.getName());
        long gap = billDate.toEpochDay() - fruit.getProduceDate().toEpochDay();
        
        double rate;
        if (gap > 7) {
            System.out.println("超过 7 天的水果请勿食用!");
            return;
        } else if (gap > 3) {
            rate = 0.5;
        } else {
            rate = 1;
        }
        double finalPrice = fruit.getPrice() * fruit.getWeight() * rate;
        System.out.printf("水果打折后价格为: %s\n", NumberFormat.getCurrencyInstance().format(finalPrice));
    }

    @Override
    public void visit(Wine wine) {
        System.out.printf("酒类: %s, 无折扣价格!\n", wine.getName());
        System.out.printf("原价售卖: %s\n", NumberFormat.getCurrencyInstance().format(wine.getPrice()));
    }
}
